package com.fastloan.mgt.model.service;

import com.fastloan.common.functioninterface.Try;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.model.ApplicationIPC;
import com.fastloan.mgt.application.base.service.ApplicationIPCService;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.application.contract.model.ApplicationContract;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.model.Model;
import com.fastloan.mgt.model.uitl.ModelUtil;
import com.fastloan.mgt.processcontrol.base.service.ProcessFunctionServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 模型计算基类
 *
 * @author yanwe
 *         createTime 2017-03-2017/3/9 15:55
 */
public abstract class GeneratorModelStrategy {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ApplicationService applicationService;

    @Autowired
    protected ZyCustomerService customerService;

    @Autowired
    protected ApplicationIPCService applicationIPCService;

    protected Map<Integer,String> codeFieldMap = new HashMap<>();

    public abstract void calculate(String applicationId, Consumer<ApplicationModel> applicationModelConsumer);

    /**
     * 回执方法
     */
    public abstract void sendBack(ApplicationContract applicationContract,boolean isPass);

    public Model getModelInfo(){
        return ModelUtil.convertModel(this);
    }

    protected Application getApplicationInfo(String applicationId){
        return applicationService.selectByPrimaryKey(applicationId);
    }


    protected ZyCustomer getCustomerInfo(String customerId){
        return customerService.selectByPrimaryKey(customerId);
    }

    protected List<ApplicationIPC> getApplicationIPC(String applicationId){
        return applicationIPCService.selectApplicationIPCNoTree(applicationId);
    }

    protected ApplicationModel createApplicationModel(String applicationId){
        ApplicationModel applicationModel = new ApplicationModel();
        applicationModel.setApplicationId(applicationId);
        applicationModel.setCreateTime(new Date());
        applicationModel.setModelId(getModelInfo().getModelId());
        applicationModel.setModelName(getModelInfo().getModelName());
        return applicationModel;
    }

    protected void convertIPC2Model(Object object,List<ApplicationIPC> applicationIPCS){
        applicationIPCS.forEach(
                Try.of((ApplicationIPC applicationIPC) ->
                        {
                            String fieldName = codeFieldMap.get(applicationIPC.getCode());
                            if (fieldName != null && !StringUtils.isEmpty(applicationIPC.getApplicationValue())){
                                try {
                                    BeanUtils.setProperty(object,fieldName
                                            ,applicationIPC.getApplicationValue());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    logger.error("模型计算IPC参数:"+fieldName+"类型错误"+applicationIPC.getApplicationValue());
                                }
                            }
                        }
                ));
    }

}
